package com.abel.quiz.common;/**
 * Created by abel.lin on 2017/8/8.
 */
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author abel.lin
 * @date_time 2017/8/8 9:09
 * @description
 */
public class ContextUtil {
    private static String projectPath;
    private static String classpath;

    /**
     * 获取 ServletRequestAttributes
     * @return
     */
    public static ServletRequestAttributes getServletRequestAttributes(){
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }

    /**
     * 获取request
     * @return
     */
    public static HttpServletRequest getRequest(){
        return getServletRequestAttributes().getRequest();
    }

    /**
     * 获取session
     * @return
     */
    public static HttpSession getSession(){
        return getRequest().getSession();
    }

    /**
     * 获取application
     * @return
     */
    public static ServletContext getApplication(){
        return getSession().getServletContext();
    }

    /**
     * 在request作用域添加一个值
     * @param name
     * @param value
     */
    public static void setRequestAttr(String name, Object value){
        getRequest().setAttribute(name, value);
    }

    /**
     * 获取reqest作用域的值
     * @param name
     * @param <T>
     * @return
     */
    public static <T> T getRequestAttr(String name){
        return (T) getRequest().getAttribute(name);
    }

    /**
     * 是删除request作用域的值
     * @param name
     */
    public static void removeRequestAttr(String name){
        getRequest().removeAttribute(name);
    }

    /**
     * 在session作用域添加一个值
     * @param name
     * @param value
     */
    public static void setSessionAttr(String name, Object value){
        getSession().setAttribute(name, value);
    }

    /**
     * 获取session作用域的值
     * @param name
     * @param <T>
     * @return
     */
    public static <T> T getSessionAttr(String name){
        return (T) getSession().getAttribute(name);
    }

    /**
     * 删除session作用域的值
     * @param name
     */
    public static void removeSession(String name){
        getSession().removeAttribute(name);
    }

    /**
     * 在application作用域添加一个值
     * @param name
     * @param value
     */
    public static void setApplicationAttr(String name, Object value){
        getApplication().setAttribute(name, value);
    }

    /**
     * 获取application作用域的值
     * @param name
     * @param <T>
     * @return
     */
    public static <T> T getApplicationAttr(String name){
        return (T) getApplication().getAttribute(name);
    }

    /**
     * 删除application作用域的值
     * @param name
     */
    public static void removeApplicationAttr(String name){
        getApplication().removeAttribute(name);
    }

    /**
     * 获取客户端的ip地址
     * @return
     */
    public static String getRemoteIpAddr() {
        HttpServletRequest request = getRequest();

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取web项目的根路径
     * @return
     */
    public static String getWebRoot(){
        if(null == projectPath){
            getClassPath();
            projectPath = classpath.substring(0, classpath.indexOf("/WEB-INF/classes"));
        }
        return projectPath;
    }

    /**
     * 获取classpath类路径
     * @return
     */
    public static String getClassPath(){
        if(null == classpath)
            classpath = ContextUtil.class.getResource("/").getPath();
        return classpath;
    }
}
